import * as Types from './mutations-type';

//状态操作中介
const mutations = {
  [Types.ADD_CART](state,book){
    //book是添加的一本书 如果已经添加了 累加数量，否则 数量为1
    let product = state.cardList.find(item=>item.bookId===book.bookId);
    if (product){
      product.bookCount += 1;//除了改属性，还要更新数组，需要替换否则不会更新state状态
      state.cardList = [...state.cardList]
    }else {
      book.bookCount = 1;
      //vux一定要改变原有数据,或者替换，不能修改
      // state.cardList.push(book);新增
      state.cardList = [...state.cardList,book];//合并一个新的
    }
  },
  [Types.DECREAMENT](state){
    state.count--;
  }
};

export default mutations;
